// Setup namespace
if (typeof(ko) == 'undefined')
    var ko = {};
if (typeof(ko.extensions) == 'undefined')
    ko.extensions = {};

// Set Logging
var log = ko.logging.getLogger("ko.extensions.webos");
//log.setLevel(ko.logging.LOG_DEBUG);

// Make webos extension namespace and add our js code
ko.extensions.webos = {};

// Get Slash
var appInfo = Components.classes["@mozilla.org/xre/app-info;1"].
getService(Components.interfaces.nsIXULRuntime);
ko.extensions.webos.slash = "\\";
if(appInfo.OS != 'WINNT')
    ko.extensions.webos.slash = "/";

//onLoad
ko.extensions.webos.onLoad = function()
{
    ko.extensions.webos.project.changed("");
    document.getElementById("bottom_splitter").setAttribute("oncommand", "ko.extensions.webos.view.resize();");
    document.getElementById("webosTabPanel").style.overflow = "hidden";
    
    // FIXES OS X issue with enviromental variables
    var appInfo = Components.classes["@mozilla.org/xre/app-info;1"].
    getService(Components.interfaces.nsIXULRuntime);
    if(appInfo.OS == "Darwin") //WINNT
    {
        var userEnvSvc = Components.classes["@activestate.com/koUserEnviron;1"].
                      getService(Components.interfaces.koIUserEnviron);
        var env = userEnvSvc.GetEncodedEnvironment();
        if(env.match(/:\/opt\/PalmSDK\/Current\/bin/) == null)
        {
            var prefsSvc = Components.classes["@activestate.com/koPrefService;1"].
                                    getService(Components.interfaces.koIPrefService);
            var prefs = prefsSvc.prefs;
            var prefStr = prefs.getStringPref("userEnvironmentStartupOverride");
            prefStr = "PATH=$PATH:/opt/PalmSDK/Current/bin";
            prefs.setStringPref("userEnvironmentStartupOverride", prefStr);
        }
    }
}

ko.extensions.webos.uiDone = false;

//Detect Komodo Events
var obsSvc = Components.classes["@mozilla.org/observer-service;1"].
                        getService(Components.interfaces.nsIObserverService);
obsSvc.addObserver(ko.extensions.webos, "current_project_changed", false);
obsSvc.addObserver(ko.extensions.webos, "run_terminated", false);
obsSvc.addObserver(ko.extensions.webos, "project_added", false);
obsSvc.addObserver(ko.extensions.webos, "project_removed", false);
obsSvc.addObserver(ko.extensions.webos, "komodo-ui-started", false);

ko.extensions.webos.observe = function(subject, topic, data)
{
    switch(topic)
    {
        case "current_project_changed":
            if(ko.extensions.webos.uiDone)
            {
                var project = ko.projects.manager.getCurrentProject();
                // This little bit of trickery is necessary in order to
                // remember the project id, as we can often get a
                // notification when the project has not changed. Ack!
                if (!project && this._activeProjectId)
                    ko.extensions.webos.project.changed("");
                else if (project && project.id != this._activeProjectId)
                    ko.extensions.webos.project.changed(project.id);
            }
            break;
        case "run_terminated":
            var retval = subject.wait(-1);
            ko.extensions.webos.palm.cb(retval, subject);
            break;
        case "project_added":
            //if(ko.extensions.webos.uiDone)
                //ko.extensions.webos.project.changed(null);
            break;
        case "project_removed":
            if(ko.extensions.webos.uiDone)
                setTimeout(ko.extensions.webos.project.changed, 500, null);
            break;
        case "komodo-ui-started":
            ko.extensions.webos.uiDone = true;
            var project = ko.projects.manager.getCurrentProject();
            ko.extensions.webos.project.changed(project.id);
            break;
    }
};

window.addEventListener("load", function(){setTimeout(ko.extensions.webos.onLoad, 500);}, false);
